package main.java.com.blog.controller;

import com.blog.entity.Art;
import com.blog.entity.Fab;
import com.blog.service.ArtService;
import com.blog.service.FabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Admin on 2017/6/9.
 */
@Controller
@RequestMapping(value = "/fab")
public class FabController {
    private static Boolean b = false;
    @Autowired
    FabService fabService;

    @Autowired
    ArtService artService;

    @ResponseBody
    @RequestMapping(value = "/addFab", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String add(String fabId, String userAccount) {
        int id = Integer.parseInt(fabId);
        Art art = artService.findById(id);
        if (fabId.equals("") || userAccount.equals("")) {
            return "请登录";
        }

        List<Fab> fabList = fabService.findAllFab();

        for (int i = 0; i < fabList.size(); i++) {
            if (fabList.get(i).getArtTitle().equals(art.getArtTitle()) &&
                    fabList.get(i).getUserAccount().equals(userAccount)) {

                b = true;
                break;
            }
        }


        if (b == true) {
            return "已赞";
        } else {
            Fab fabOne = new Fab();
            fabOne.setArtTitle(art.getArtTitle());
            fabOne.setUserAccount(userAccount);
            fabOne.setFabNumber(1);
            fabService.addFab(fabOne);
            return "赞";
        }


    }
}
